package com.zytech.zspider.logger;

import com.google.common.collect.Maps;
import com.zytech.modules.lang.Requires;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentMap;

/**
 * @author siuming
 */
public final class Loggers {

    private static final Loggers ME = new Loggers();
    private final ConcurrentMap<Class<?>, Logger> loggers = Maps.newConcurrentMap();

    /**
     * @return
     */
    public static Loggers me() {
        return ME;
    }

    /**
     * @param type
     * @param msg
     * @param e
     */
    public void debug(Class<?> type, String msg, Throwable e) {
        ofLogger(type).debug(msg, e);
    }

    /**
     * @param type
     * @param msg
     * @param objs
     */
    public void debug(Class<?> type, String msg, Object... objs) {
        ofLogger(type).debug(msg, objs);
    }

    /**
     * @param type
     * @param msg
     * @param e
     */
    public void info(Class<?> type, String msg, Throwable e) {
        ofLogger(type).info(msg, e);
    }

    /**
     * @param type
     * @param msg
     * @param objs
     */
    public void info(Class<?> type, String msg, Object... objs) {
        ofLogger(type).info(msg, objs);
    }


    /**
     * @param type
     * @param msg
     * @param e
     */
    public void warn(Class<?> type, String msg, Throwable e) {
        ofLogger(type).warn(msg, e);
    }

    /**
     * @param type
     * @param msg
     * @param objs
     */
    public void warn(Class<?> type, String msg, Object... objs) {
        ofLogger(type).warn(msg, objs);
    }

    /**
     * @param type
     * @param msg
     * @param e
     */
    public void error(Class<?> type, String msg, Throwable e) {
        ofLogger(type).error(msg, e);
    }

    /**
     * @param type
     * @param msg
     * @param objs
     */
    public void error(Class<?> type, String msg, Object... objs) {
        ofLogger(type).error(msg, objs);
    }

    private Logger ofLogger(Class<?> type) {
        Requires.notNull(type, "type must not be null.");
        if (null == loggers.get(type)) {
            loggers.putIfAbsent(type, LoggerFactory.getLogger(type));
        }

        return loggers.get(type);
    }

    private Loggers() {
    }
}
